package com.book.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.book.common.annotation.RepeatSubmit;
import com.book.common.annotation.group.UpdateGroup;
import com.book.common.core.controller.BaseController;
import com.book.common.core.domain.AjaxResult;
import com.book.common.core.domain.entity.AppUser;
import com.book.common.core.page.TableDataInfo;
import com.book.common.utils.StringUtils;
import com.book.system.service.IAppUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author Bigboss
 * @create 2024-11-10 19:52
 */
@Api(tags = "app用户管理")
@RestController
@RequestMapping("/system/appUser")
public class SysAppUserController extends BaseController {

    private final IAppUserService appUserService;

    public SysAppUserController(IAppUserService appUserService) {
        this.appUserService = appUserService;
    }

    /**
     * 获取app用户列表
     * @return
     */
    @ApiOperation(value = "获取app用户列表，权限：system:appUser:list")
    @PreAuthorize("@ss.hasPermi('system:appUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppUser user)
    {
        startPage();
        LambdaQueryWrapper<AppUser> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(user.getPhone())){
            queryWrapper.like(AppUser::getPhone,user.getPhone());
        }
        if(StringUtils.isNotEmpty(user.getNickName())){
            queryWrapper.like(AppUser::getNickName,user.getNickName());
        }
        if(StringUtils.isNotEmpty(user.getName())){
            queryWrapper.like(AppUser::getName,user.getName());
        }
        return getDataTable(appUserService.list(new LambdaQueryWrapper<AppUser>().orderByAsc(AppUser::getCreateTime)));
    }


    /**
     * 修改app用户信息
     * @param user
     * @return
     */
    @ApiOperation(value = "修改app用户信息，权限：system:appUser:edit")
    @PreAuthorize("@ss.hasPermi('system:appUser:edit')")
    @RepeatSubmit
    @PutMapping("/updateInfo")
    public AjaxResult updateInfo(@RequestBody @Validated({UpdateGroup.class}) AppUser user){
        if (StringUtils.isNotEmpty(user.getPhone()) && !appUserService.checkPhoneUnique(user)) {
            return AjaxResult.error("修改用户信息失败，手机号码已存在");
        }
        return appUserService.updateById(user) ? success() : error();
    }
}
